package com.qfedu.mapper;

import com.qfedu.model.Project;
import com.qfedu.model.SubItem;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 概述:
 *
 * @Author: Tbh
 * @Date: 2022/7/3 0:06
 */

public interface SubItemMapper {

    @Results(id = "subItemMap",value = {
            @Result(column = "suid",property = "suid"),
            @Result(column = "sname",property = "sname"),
            @Result(column = "starttime",property = "starttime"),
            @Result(column = "endtime",property = "endtime"),
            @Result(column = "updatetime",property = "updatetime"),
            @Result(column = "pid",property = "pid"),
            @Result(column = "pid",property = "project",javaType = Project.class,one =  //一对一关联查询
            @One(select = "com.qfedu.mapper.ProjectMapper.findProject",fetchType = FetchType.EAGER)
            ),
    })
    @Select("select * from subitem where status = 1 limit #{currentPage},#{pageSize}")
    List<SubItem> findPage(@Param("currentPage") int currentPage, @Param("pageSize") Integer pageSize);

    @Select("select count(*) from subitem where status = 1")
    Long count();

    @Insert("insert into subitem (suid,sname,starttime,endtime,updatetime,pid) " +
            "values (#{suid},#{sname},#{starttime},#{endtime},now(),#{pid})")
    int addSubItem(SubItem subItem);

    @ResultMap("subItemMap")
    @Select("select * from subitem where suid = #{suid} and status = 1")
    SubItem findBySuid(String suid);

    @Update("update subitem set sname = #{sname},pid = #{pid} where suid = #{suid} and status = 1")
    int updateSubItem(SubItem subItem);

    @Update("update subitem set status = 0 where suid = #{suid} ")
    int deleteBySuid(String suid);

    @Select("Select * from subitem where status = 1")
    List<SubItem> findAll();
}
